arithmetic-parser 0.2.0

Parser for arithmetic expressions with flexible literals and type annotations.
Documentation

Flexible Arithmetic Parser

Build Status License: Apache-2.0 rust 1.44+ required

Links: Docs.rs crate docs (master) changelog

A versatile parser for arithmetic expressions which allows customizing literal definitions, type annotations and several other aspects of parsing.

Usage

Add this to your Crate.toml:

[dependencies]
arithmetic-parser = "0.2.0"

The parser is overall similar to Rust. It supports variables, literals, comments, arithmetic and boolean operations, parentheses, function calls, tuples and tuple destructuring, function definitions, blocks, methods, and type annotations.

See the crate docs for more details on the supported features.

Code Sample

Here is an example of code parsed with the grammar with real-valued literals and the only supported type Num:

// This is a comment.
x = 1 + 2.5 * 3 + sin(a^3 / b^2);
// Function declarations have syntax similar to Rust closures.
some_function = |a, b: Num| (a + b, a - b);
other_function = |x| {
    r = min(rand(), 0.5);
    r * x
};
// Tuples and blocks are supported and have a similar syntax to Rust.
(y, z: Num) = some_function({ x = x - 0.5; x }, x);
other_function(y - z)

Implementation Details

The parser is based on the nom crate. The core trait of the library, Grammar, is designed in such a way that switching optional features should not induce run-time overhead; the unused parsing code paths should be removed during compilation.

License

Licensed under the Apache-2.0 license.